<?php
/**
 * Created by PhpStorm.
 * User: acker
 * Date: 21-06-08
 * Time: 下午6:25
 */

namespace App\Models\yb;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\DefaultDatetimeFormat;


class UserWaterTicketModel extends Model
{
    use DefaultDatetimeFormat;

    use SoftDeletes;

    /**
     *
     *计量单位
     * @var string
     */
    protected $table = 'yb_user_water_ticket';

    /**
     * 主键
     *
     * @var string
     */
    protected $primaryKey = 'id';


    /**
     * 该模型是否被自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 自定义字段
     *
     * @var bool
     */
    protected $appends = ['goods_specs','cumulative_number','cover_transferor_number','transferor_number','used_number'];

    /**
     * 规格
     * @return mixed
     */
    public function getGoodsSpecsAttribute(){
        $goods_id = $this->goods_id;
        $goods_data = GoodsModel::where(['id'=>$goods_id])->first();
        if(empty($goods_data)){
            return "";
        }else{
                 return $goods_data['goods_title']."&nbsp;&nbsp;&nbsp;&nbsp;".$goods_data['goods_specs'];
   
        }
    }

    /**
     * 累计购买
     * @return mixed
     */
    public function getCumulativeNumberAttribute(){
        $user_id = $this->user_id;
        $water_ticket_id= $this->water_ticket_id;

        $number = UserWaterTicketRecordModel::where(['user_id'=>$user_id,'water_ticket_id'=>$water_ticket_id,'type'=>2,'status'=>1])->sum('number');

        return $number;
    }


    /**
     * 被转让获得
     * @return mixed
     */
    public function getCoverTransferorNumberAttribute(){
        $user_id = $this->user_id;
        $water_ticket_id= $this->water_ticket_id;

        $number = UserWaterTicketRecordModel::where(['user_id'=>$user_id,'water_ticket_id'=>$water_ticket_id,'type'=>3,'status'=>1])->sum('number');

        return $number;
    }


    /**
     * 转让消耗
     * @return mixed
     */
    public function getTransferorNumberAttribute(){
        $user_id = $this->user_id;
        $water_ticket_id= $this->water_ticket_id;

        $number = UserWaterTicketRecordModel::where(['transferor_id'=>$user_id,'water_ticket_id'=>$water_ticket_id,'type'=>3,'status'=>0])->sum('number');

        return $number;
    }

    /**
     * 已使用
     * @return mixed
     */
    public function getUsedNumberAttribute(){
        $user_id = $this->user_id;
        $water_ticket_id= $this->water_ticket_id;

        $number = UserWaterTicketRecordModel::where(['user_id'=>$user_id,'water_ticket_id'=>$water_ticket_id,'type'=>1,'status'=>0])->sum('number');

        return $number;
    }



}
